package com.gomtel.smartdevice.moudle.db;

import android.database.sqlite.SQLiteDatabase;


import com.gomtel.smartdevice.global.WatchApplication;
import com.gomtel.smartdevice.moudle.db.entity.AppConfigurationInfo;
import com.gomtel.smartdevice.moudle.db.entity.AppConfigurationInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.AtrialFibrillationInfo;
import com.gomtel.smartdevice.moudle.db.entity.AtrialFibrillationInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.BdDataInfo;
import com.gomtel.smartdevice.moudle.db.entity.BdDataInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.CaloryDataInfo;
import com.gomtel.smartdevice.moudle.db.entity.CaloryDataInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.DaoMaster;
import com.gomtel.smartdevice.moudle.db.entity.DaoSession;
import com.gomtel.smartdevice.moudle.db.entity.EcgDataInfo;
import com.gomtel.smartdevice.moudle.db.entity.EcgDataInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.HeartRateDataInfo;
import com.gomtel.smartdevice.moudle.db.entity.HeartRateDataInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.LoginUserInfo;
import com.gomtel.smartdevice.moudle.db.entity.LoginUserInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.PathLengthContentInfo;
import com.gomtel.smartdevice.moudle.db.entity.PathLengthContentInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.PathLengthInfo;
import com.gomtel.smartdevice.moudle.db.entity.PathLengthInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.PulseConditionBean;
import com.gomtel.smartdevice.moudle.db.entity.PulseConditionBeanDao;
import com.gomtel.smartdevice.moudle.db.entity.SignsOfDataInfo;
import com.gomtel.smartdevice.moudle.db.entity.SignsOfDataInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.StepCountContentInfo;
import com.gomtel.smartdevice.moudle.db.entity.StepCountContentInfoDao;
import com.gomtel.smartdevice.moudle.db.entity.StepCountInfo;
import com.gomtel.smartdevice.moudle.db.entity.StepCountInfoDao;
import com.gomtel.smartdevice.widget.Constants;

import java.util.List;

import javax.inject.Inject;


/**
 * Created by Administrator on 2018/2/11.
 */

public class GreenDaoHelper implements DBHelper {

    private final LoginUserInfoDao loginUserInfoDao;
    private DaoSession mDaoSession;
    private final AppConfigurationInfoDao appConfigurationBeanDao;
    private final StepCountInfoDao stepCountInfoDao;
    private final StepCountContentInfoDao stepCountContentInfoDao;
    private final PathLengthInfoDao pathLengthInfoDao;
    private final PathLengthContentInfoDao pathLengthContentInfoDao;
    private final SignsOfDataInfoDao signsOfDataInfoDao;
    private final EcgDataInfoDao ecgDataInfoDao;
    private final BdDataInfoDao bdDataInfoDao;
    private final CaloryDataInfoDao caloryDataInfoDao;
    private final HeartRateDataInfoDao heartRateDataInfoDao;
    private final PulseConditionBeanDao pulseConditionBeanDao;
    private final AtrialFibrillationInfoDao atrialFibrillationInfoDao;

    public LoginUserInfoDao getLoginUserInfoDao() {
        return loginUserInfoDao;
    }

    public DaoSession getmDaoSession() {
        return mDaoSession;
    }

    public AppConfigurationInfoDao getAppConfigurationBeanDao() {
        return appConfigurationBeanDao;
    }

    public StepCountInfoDao getStepCountInfoDao() {
        return stepCountInfoDao;
    }

    public StepCountContentInfoDao getStepCountContentInfoDao() {
        return stepCountContentInfoDao;
    }

    public PathLengthInfoDao getPathLengthInfoDao() {
        return pathLengthInfoDao;
    }

    public PathLengthContentInfoDao getPathLengthContentInfoDao() {
        return pathLengthContentInfoDao;
    }

    public SignsOfDataInfoDao getSignsOfDataInfoDao() {
        return signsOfDataInfoDao;
    }

    public EcgDataInfoDao getEcgDataInfoDao() {
        return ecgDataInfoDao;
    }

    public BdDataInfoDao getBdDataInfoDao() {
        return bdDataInfoDao;
    }

    public CaloryDataInfoDao getCaloryDataInfoDao() {
        return caloryDataInfoDao;
    }

    public HeartRateDataInfoDao getHeartRateDataInfoDao() {
        return heartRateDataInfoDao;
    }

    public PulseConditionBeanDao getPulseConditionBeanDao() {
        return pulseConditionBeanDao;
    }

    public AtrialFibrillationInfoDao getAtrialFibrillationInfoDao() {
        return atrialFibrillationInfoDao;
    }

    @Inject
    public GreenDaoHelper() {
        //初始化数据库
        setupDatabase();
        loginUserInfoDao = mDaoSession.getLoginUserInfoDao();
        appConfigurationBeanDao = mDaoSession.getAppConfigurationInfoDao();
        stepCountInfoDao = mDaoSession.getStepCountInfoDao();
        stepCountContentInfoDao = mDaoSession.getStepCountContentInfoDao();
        pathLengthInfoDao = mDaoSession.getPathLengthInfoDao();
        pathLengthContentInfoDao = mDaoSession.getPathLengthContentInfoDao();
        signsOfDataInfoDao = mDaoSession.getSignsOfDataInfoDao();
        ecgDataInfoDao = mDaoSession.getEcgDataInfoDao();
        bdDataInfoDao = mDaoSession.getBdDataInfoDao();
        caloryDataInfoDao = mDaoSession.getCaloryDataInfoDao();
        heartRateDataInfoDao = mDaoSession.getHeartRateDataInfoDao();
        pulseConditionBeanDao = mDaoSession.getPulseConditionBeanDao();
        atrialFibrillationInfoDao = mDaoSession.getAtrialFibrillationInfoDao();

    }

    private void setupDatabase() {
        //创建数据库,DevOpenHelper：创建SQLite数据库的SQLiteOpenHelper的具体实现
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(WatchApplication.getInstance().getApplicationContext(), Constants.DATABASE_USER_NAME, null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象,DaoMaster：GreenDao的顶级对象，作为数据库对象、用于创建表和删除表
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者,DaoSession：管理所有的Dao对象，Dao对象中存在着增删改查等API
        mDaoSession = daoMaster.newSession();
    }

    @Override
    public LoginUserInfo queryLoginUserInfo(String mUserName) {
        mDaoSession.clear();
        return loginUserInfoDao.queryBuilder().where(LoginUserInfoDao.Properties.Username.eq(mUserName)).build().unique();
    }

    @Override
    public LoginUserInfo queryLoginUserInfoToRecentlyTime(int recentlyTime) {
        mDaoSession.clear();
//        loginUserInfoDao.queryBuilder().where(LoginUserInfoDao.Properties.LoginIndex.eq(recentlyTime)).build().unique()
        return null;
    }

    @Override
    public AppConfigurationInfo queryAppConfigurationInfo(String currentUser) {
        mDaoSession.clear();
        return appConfigurationBeanDao.queryBuilder().where(AppConfigurationInfoDao.Properties.CurrentUser.eq(currentUser)).unique();
    }

    @Override
    public StepCountInfo queryCurrentDataStepInfo(String CurrentData) {
        mDaoSession.clear();
        return stepCountInfoDao.queryBuilder().where(StepCountInfoDao.Properties.Data.eq(CurrentData)).unique();
    }

    @Override
    public List<StepCountInfo> queryCurrentWeekStepInfo(String CurrentWeek) {
        mDaoSession.clear();
        return stepCountInfoDao.queryBuilder().where(StepCountInfoDao.Properties.WeekFirst.eq(CurrentWeek)).list();
    }

    @Override
    public List<StepCountInfo> queryCurrentMonthStepInfo(String CurrentMonth) {
        mDaoSession.clear();
        return stepCountInfoDao.queryBuilder().where(StepCountInfoDao.Properties.MonthCurrent.eq(CurrentMonth)).list();
    }

    @Override
    public List<StepCountContentInfo> queryStepCountContentInfo(String data) {
        mDaoSession.clear();
        return stepCountContentInfoDao.queryBuilder().where(StepCountContentInfoDao.Properties.Data.eq(data)).list();
    }

    @Override
    public StepCountContentInfo queryStepCountCurrentTimeContentInfo(String currentTime) {
        mDaoSession.clear();
        return stepCountContentInfoDao.queryBuilder().where(StepCountContentInfoDao.Properties.CurrentTime.eq(currentTime)).unique();
    }

    @Override
    public PathLengthInfo queryCurrentDataLengthInfo(String CurrentData) {
        mDaoSession.clear();
        return pathLengthInfoDao.queryBuilder().where(PathLengthInfoDao.Properties.Data.eq(CurrentData)).unique();
    }

    @Override
    public List<PathLengthInfo> queryCurrentWeekLengthInfo(String CurrentWeek) {
        mDaoSession.clear();
        return pathLengthInfoDao.queryBuilder().where(PathLengthInfoDao.Properties.WeekFirst.eq(CurrentWeek)).list();
    }

    @Override
    public List<PathLengthInfo> queryCurrentMonthLengthInfo(String CurrentMonth) {
        mDaoSession.clear();
        return pathLengthInfoDao.queryBuilder().where(PathLengthInfoDao.Properties.MonthCurrent.eq(CurrentMonth)).list();
    }

    @Override
    public List<PathLengthContentInfo> queryPathLengthContentInfo(String data) {
        mDaoSession.clear();
        return pathLengthContentInfoDao.queryBuilder().where(PathLengthContentInfoDao.Properties.Data.eq(data)).list();
    }

    @Override
    public PathLengthContentInfo queryPathLengthCurrentTimeContentInfo(String currentTime) {
        mDaoSession.clear();
        return pathLengthContentInfoDao.queryBuilder().where(PathLengthContentInfoDao.Properties.CurrentTime.eq(currentTime)).unique();
    }

    @Override
    public SignsOfDataInfo querySignsOfDataInfo(String currentUser) {
        mDaoSession.clear();
        return signsOfDataInfoDao.queryBuilder().where(SignsOfDataInfoDao.Properties.UserName.eq(currentUser)).unique();
    }

    @Override
    public List<EcgDataInfo> queryEcgDataInfo(String currentUser) {
        mDaoSession.clear();
        return ecgDataInfoDao.queryBuilder().where(EcgDataInfoDao.Properties.Username.eq(currentUser)).list();
    }

    @Override
    public List<BdDataInfo> queryBdDataInfo(String currentUser) {
        mDaoSession.clear();
        return bdDataInfoDao.queryBuilder().where(BdDataInfoDao.Properties.Username.eq(currentUser)).list();
    }

    @Override
    public CaloryDataInfo queryCaloryDataInfo(String day) {
        mDaoSession.clear();
        return caloryDataInfoDao.queryBuilder().where(CaloryDataInfoDao.Properties.Day.eq(day)).unique();
    }

    @Override
    public HeartRateDataInfo queryHeartRateDataInfo(String day) {
        mDaoSession.clear();
        return heartRateDataInfoDao.queryBuilder().where(HeartRateDataInfoDao.Properties.Day.eq(day)).unique();
    }

    @Override
    public List<HeartRateDataInfo> queryHeartRateDataList(String currentUser) {
        mDaoSession.clear();
        return heartRateDataInfoDao.queryBuilder().where(HeartRateDataInfoDao.Properties.UserName.eq(currentUser)).list();
    }

    @Override
    public List<PulseConditionBean> queryPulseConditionList(String currentUser) {
        mDaoSession.clear();
        return pulseConditionBeanDao.queryBuilder().where(PulseConditionBeanDao.Properties.CurrentUser.eq(currentUser)).list();
    }

    @Override
    public List<PulseConditionBean> queryPulseConditionSelectTimeList(String day) {
        mDaoSession.clear();
        return pulseConditionBeanDao.queryBuilder().where(PulseConditionBeanDao.Properties.Day.eq(day)).list();
    }

    @Override
    public List<PulseConditionBean> queryPulseConditionAllList() {
        mDaoSession.clear();
        return pulseConditionBeanDao.loadAll();
    }

    @Override
    public List<AtrialFibrillationInfo> queryAtrialFibrillationInfo(String day) {
        mDaoSession.clear();
        return atrialFibrillationInfoDao.queryBuilder().where(AtrialFibrillationInfoDao.Properties.RecentlyTime.eq(day)).list();
    }

    @Override
    public List<AtrialFibrillationInfo> queryAtrialFibrillationALLInfo(String currentUser) {
        mDaoSession.clear();
        return atrialFibrillationInfoDao.queryBuilder().where(AtrialFibrillationInfoDao.Properties.Username.eq(currentUser)).list();
    }

    @Override
    public List<LoginUserInfo> loadLoginUserInfo() {
        return loginUserInfoDao.loadAll();
    }

    @Override
    public List<AppConfigurationInfo> loadAppConfigurationInfo() {
        return appConfigurationBeanDao.loadAll();
    }

    @Override
    public void insertLoginUserInfo(LoginUserInfo mLoginUserInfo) {
        loginUserInfoDao.insertOrReplace(mLoginUserInfo);
    }

    @Override
    public void insertAppConfigurationInfo(AppConfigurationInfo appConfigurationInfo) {
        appConfigurationBeanDao.insertOrReplace(appConfigurationInfo);
    }

    @Override
    public void insertStepCountInfo(StepCountInfo stepCountInfo) {
        stepCountInfoDao.insertOrReplace(stepCountInfo);
    }

    @Override
    public void insertStepCountContentInfo(StepCountContentInfo stepCountContentInfo) {
        stepCountContentInfoDao.insertOrReplace(stepCountContentInfo);
    }

    @Override
    public void insertPathLengthInfo(PathLengthInfo pathLengthInfo) {
        pathLengthInfoDao.insertOrReplace(pathLengthInfo);
    }

    @Override
    public void insertPathLengthContentInfo(PathLengthContentInfo pathLengthContentInfo) {
        pathLengthContentInfoDao.insertOrReplace(pathLengthContentInfo);
    }

    @Override
    public void insertSignsOfDataInfo(SignsOfDataInfo signsOfDataInfo) {
        signsOfDataInfoDao.insertOrReplace(signsOfDataInfo);
    }

    @Override
    public void insertEcgDataInfo(EcgDataInfo ecgDataInfo) {
        ecgDataInfoDao.insertOrReplace(ecgDataInfo);
    }

    @Override
    public void insertBdDataInfo(BdDataInfo bdDataInfo) {
        bdDataInfoDao.insertOrReplace(bdDataInfo);
    }

    @Override
    public void insertCaloryDataInfo(CaloryDataInfo caloryDataInfo) {
        caloryDataInfoDao.insertOrReplace(caloryDataInfo);
    }

    @Override
    public void insertHeartRateDataInfo(HeartRateDataInfo heartRateDataInfo) {
        heartRateDataInfoDao.insertOrReplace(heartRateDataInfo);
    }

    @Override
    public void insertPulseConditionBean(PulseConditionBean pulseConditionBean) {
        pulseConditionBeanDao.insertOrReplace(pulseConditionBean);
    }

    @Override
    public void insertAtrialFibrillationInfo(AtrialFibrillationInfo atrialFibrillationInfo) {
        atrialFibrillationInfoDao.insertOrReplace(atrialFibrillationInfo);
    }

    @Override
    public void deleteLoginUserInfo(String mUserName) {
        LoginUserInfo loginUserInfo = loginUserInfoDao.queryBuilder().where(LoginUserInfoDao.Properties.Username.eq(mUserName)).build().unique();
        if (loginUserInfo != null)
            loginUserInfoDao.delete(loginUserInfo);
    }

    @Override
    public void deleteAppConfigurationInfo(String mUserName) {
        mDaoSession.clear();
        AppConfigurationInfo appConfigurationInfo = appConfigurationBeanDao.queryBuilder().where(AppConfigurationInfoDao.Properties.CurrentUser.eq(mUserName)).build().unique();
        if (appConfigurationInfo != null)
            appConfigurationBeanDao.delete(appConfigurationInfo);
    }

    @Override
    public void UpDataLoginUserInfo(LoginUserInfo mLoginUserInfo) {
        loginUserInfoDao.update(mLoginUserInfo);
    }

    @Override
    public void UpDataAppConfigurationInfo(AppConfigurationInfo appConfigurationInfo) {
        appConfigurationBeanDao.update(appConfigurationInfo);
    }

    @Override
    public void UpDataStepCountInfo(StepCountInfo stepCountInfo) {
        stepCountInfoDao.update(stepCountInfo);
    }

    @Override
    public void UpDataStepCountContentInfo(StepCountContentInfo stepCountContentInfo) {
        stepCountContentInfoDao.update(stepCountContentInfo);
    }

    @Override
    public void UpDataPathLengthInfo(PathLengthInfo pathLengthInfo) {
        pathLengthInfoDao.update(pathLengthInfo);
    }

    @Override
    public void UpDataPathLengthContentInfo(PathLengthContentInfo pathLengthContentInfo) {
        pathLengthContentInfoDao.update(pathLengthContentInfo);
    }

    @Override
    public void UpDataSignsOfDataInfo(SignsOfDataInfo signsOfDataInfo) {
        signsOfDataInfoDao.update(signsOfDataInfo);
    }

    @Override
    public void UpDataEcgDataInfo(EcgDataInfo ecgDataInfo) {
        ecgDataInfoDao.update(ecgDataInfo);
    }

    @Override
    public void UpDataBdDataInfo(BdDataInfo bdDataInfo) {
        bdDataInfoDao.update(bdDataInfo);
    }

    @Override
    public void UpDataCaloryDataInfo(CaloryDataInfo caloryDataInfo) {
        caloryDataInfoDao.update(caloryDataInfo);
    }

    @Override
    public void UpDataHeartRateDataInfo(HeartRateDataInfo heartRateDataInfo) {
        heartRateDataInfoDao.update(heartRateDataInfo);
    }
}
